class_name LockDoor
extends Node2D


@onready var sprite_2d: Sprite2D = $Sprite2D
@export var close_width: float = 1
@export var open_width: float = 12
@export var height: float = 16

var is_player_entered: bool = false

@onready var static_collision: CollisionShape2D = $StaticBody2D/CollisionShape2D
@onready var interact_colision: CollisionShape2D = $InteractArea/CollisionShape2D
@onready var light_occluder_2d: LightOccluder2D = $Sprite2D/LightOccluder2D

@export var open_color: Color
@export var close_color: Color

@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var key_tip: Sprite2D = $KeyTip

var door_data: DoorData

var level: Level = null


func _ready() -> void:
	static_collision.shape = static_collision.shape.duplicate()
	interact_colision.shape = interact_colision.shape.duplicate()
	
	if not door_data:
		door_data = DoorData.new()
	door_data.door_id = self.name
	
	open_width = height / 2.0
	
	sprite_2d.scale = Vector2(close_width, height)
	close()


func init_by_data(data: DoorData):
	door_data = data
	if door_data.is_open:
		open()
	else:
		close()


func set_static_collision_close():
	var shape = static_collision.shape as RectangleShape2D
	shape.size.x = close_width
	shape.size.y = height
	static_collision.position = Vector2(close_width, height) / 2.0


func set_interact_area_close():
	var shape = interact_colision.shape as RectangleShape2D
	shape.size.x = close_width + 32
	shape.size.y = height
	interact_colision.position = Vector2(close_width, height) / 2.0
	

func set_interact_area_open():
	var shape = interact_colision.shape as RectangleShape2D
	shape.size.x = open_width + 32
	shape.size.y = height
	interact_colision.position = Vector2(open_width, height) / 2.0


func is_open():
	return door_data.is_open


func open():
	door_data.is_open = true
	static_collision.disabled = true
	set_interact_area_open()
	light_occluder_2d.visible = false
	
	var tween = create_tween()
	tween.tween_property(sprite_2d, "scale:x", open_width, 0.3) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_IN_OUT).from_current()
	
	tween = create_tween()
	tween.tween_property(sprite_2d, "modulate", open_color, 0.3) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_IN_OUT).from_current()


func close():
	door_data.is_open = false
	static_collision.disabled = false
	set_static_collision_close()
	
	set_interact_area_close()
	light_occluder_2d.visible = true
	
	var tween = create_tween()
	tween.tween_property(sprite_2d, "scale:x", close_width, 0.3) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_IN_OUT).from_current()
	
	tween = create_tween()
	tween.tween_property(sprite_2d, "modulate", close_color, 0.3) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_IN_OUT).from_current()
	

func show_key_tip():
	animation_player.stop()
	animation_player.play("key_tip")
	
	var tween = create_tween()
	tween.tween_property(key_tip, "modulate:a", 1.0, 0.3) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_OUT).from_current()
	

func hide_key_tip():
	var tween = create_tween()
	tween.tween_property(key_tip, "modulate:a", 0.0, 0.3) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_IN).from_current()


func _on_interact_area_body_entered(body: Node2D) -> void:
	is_player_entered = true
	if door_data.is_lock:
		show_key_tip()


func _on_interact_area_body_exited(body: Node2D) -> void:
	is_player_entered = false
	hide_key_tip()


func _process(delta: float) -> void:
	process_open_close_door()
		

func disable_physics():
	interact_colision.disabled = true
	static_collision.disabled = true


func enable_physics():
	interact_colision.disabled = false
	static_collision.disabled = door_data.is_open
		
		
func process_open_close_door():
	if Input.is_action_just_pressed("interact") and is_player_entered:
		if door_data.is_lock:
			try_unlock()
		else:
			if is_open():
				close()
			else:
				open()
				

func try_unlock():
	var level = get_tree().current_scene as Level
	var player = level.player
	
	if player.player_data.little_key_num > 0:
		player.player_data.little_key_num -= 1
		door_data.is_lock = false
		hide_key_tip()
		
		GlobalSignals.player_info_changed.emit()
	
	
	
